﻿Imports System.Data.OleDb
Public Class EditProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MacrgraphyConnectionString").ConnectionString)
            Dim sql = "SELECT * FROM Portfolio WHERE UserName =@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            conn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("First_ Name")) Then txtFname.Text = dr("First_ Name")
                If Not IsDBNull(dr("Last_Name")) Then txtLname.Text = dr("Last_Name")
                If Not IsDBNull(dr("Description")) Then txtDesc.Text = dr("Description")
            End If
        End If
    End Sub

    Protected Sub Btnsave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btnsave.Click

        Dim SqlString As String
        Dim A As Integer


        Dim myGUID = Guid.NewGuid()
        Dim newFileName As String = myGUID.ToString() & ".jpg"

        Dim picLocationOnServerHardDisk = Request.MapPath("images") & "/" & newFileName
        Picupload.SaveAs(picLocationOnServerHardDisk)



        Dim oleDb As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MacrgraphyConnectionString").ConnectionString)


        SqlString = "UPDATE Portfolio SET Portfolio.[First_ Name] = @f1, Portfolio.Last_Name = @f2, Portfolio.Picture = @f3, Portfolio.Description =@f4 WHERE Portfolio.UserName=@f5"

       


        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDb)
        oleDb.Open()

        cmd.CommandType = CommandType.Text

        cmd.Parameters.AddWithValue("@f1", txtFname.Text)
        cmd.Parameters.AddWithValue("@f2", txtLname.Text)

        cmd.Parameters.AddWithValue("@f3", "images/" & newFileName)
        cmd.Parameters.AddWithValue("@f4", txtDesc.Text)
        cmd.Parameters.AddWithValue("@f5", User.Identity.Name)

        A = cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDb.Close()
        oleDb.Dispose()
        'Response.Redirect("Profile.aspx?UserName=" & User.Identity.Name)
        Response.Redirect("Profile.aspx?UserName=" & User.Identity.Name)

    End Sub
End Class